// Array.flat(n)  指定n为展开的层数

// const arr = [1,2,3, [4,5], [[6,7]], [[[8,9]]]];
// console.log(arr.flat(2));   // [ 1, 2, 3, 4, 5, 6, 7, [ 8, 9 ] ]

function myFlat(arr, depth) {
    if (!Array.isArray(arr) || depth <= 0) {
        return arr;
    };


    return arr.reduce((prev, cur) => {
        if (Array.isArray(cur)) {
            return prev.concat(myFlat(cur, depth - 1));
        } else {
            return prev.concat(cur);
        }
    }, []);
};

const arr = [1,2,3, [4,5], [[6,7]], [[[8,9]]]];
console.log(myFlat(arr, 2));   // [ 1, 2, 3, 4, 5, 6, 7, [ 8, 9 ] ]

